package com.accenture.fd.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.accenture.fd.entity.DesignEntity;
import com.accenture.fd.entity.ItemControlEntity;
import com.accenture.fd.entity.ScreenItemDescriptionEntity;
import com.accenture.fd.service.IntegrityCheckService;

import cn.hutool.core.util.StrUtil;

@CrossOrigin
@RestController
public class IntegrityCheckController {

	@Autowired
	private IntegrityCheckService integrityCheckService;

	/**
	 * 仕様書一覧取得
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/getDesignList")
	public List<DesignEntity> getDesignList(HttpServletRequest request) {

		String designTypeId = "";
		if(request.getParameterMap().get("designTypeId")[0].equals("1")) {
			designTypeId = "画面項目記述書";
		}else if(request.getParameterMap().get("designTypeId")[0].equals("2")) {
			designTypeId = "処理一覧";
		}else if(request.getParameterMap().get("designTypeId")[0].equals("3")) {
			designTypeId = "画面間共有データ";
		}else {
			designTypeId = "項目移送記述書";
		}

		// check list
		List<DesignEntity> designList = integrityCheckService.getDesignList(request.getParameterMap().get("screenId")[0],
				designTypeId);
		return designList;
	}

	/**
	 * 画面項目記述書 項目一覧
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/getScreenItemDescriptionList")
	public List<ScreenItemDescriptionEntity> getScreenItemDescriptionList(HttpServletRequest request) {

		//
		List<ScreenItemDescriptionEntity> screenItemDescriptionList = integrityCheckService
				.getScreenItemDescriptionList(request.getParameterMap().get("screenId")[0],
						request.getParameterMap().get("designTypeId")[0]);
		return screenItemDescriptionList;
	}
	
	@RequestMapping("/getScreenIdList1")
	public List<String> getScreenIdList(HttpServletRequest request) {

		List<String> returnValue = integrityCheckService.getScreenIdList();

		return returnValue;
	}

	/**
	 * チェック
	 *
	 * @param screenItems
	 * @return
	 */
	@PostMapping("/integrityCheck")
	public List<ScreenItemDescriptionEntity> integrityCheck(
			@RequestBody List<ScreenItemDescriptionEntity> screenItemDescriptions) {

		boolean errFlg = false;
		
		List<String> logicalDataItemNameList = integrityCheckService.getLogicalDataItemName();

		for (ScreenItemDescriptionEntity item : screenItemDescriptions) {
			if (!StrUtil.isBlank(item.getDataItem()) && !logicalDataItemNameList.contains(item.getDataItem())) {
				item.setCheckFlg(true);
				errFlg = true;
			}
		}

		if (errFlg) {
			integrityCheckService.updateStatus(screenItemDescriptions.get(0).getScreenId(),
					screenItemDescriptions.get(0).getDesignTypeId());
		}

		return screenItemDescriptions;
	}
	
	/**
	 * チェック2
	 *
	 * @param screenItems
	 * @return
	 */
	@PostMapping("/integrityCheck2")
	public List<ScreenItemDescriptionEntity> integrityCheck2(
			@RequestBody List<ScreenItemDescriptionEntity> screenItemDescriptions) {

		List<String> processidList = integrityCheckService.getprocessid();

		for (ScreenItemDescriptionEntity item : screenItemDescriptions) {
			if (!StrUtil.isBlank(item.getProcessingId()) && !processidList.contains(item.getProcessingId())) {
				item.setCheckFlg2(true);
			}
		}

		return screenItemDescriptions;
	}
	
	/**
	 * チェック3
	 * 
	 * @param screenItems
	 * @return
	 */
	@PostMapping("/integrityCheck3")
	public List<ItemControlEntity> integrityCheck3(
			@RequestBody List<ScreenItemDescriptionEntity> screenItemDescriptionList) {
        //dummy code
		if(screenItemDescriptionList.isEmpty()) {
			return Collections.emptyList(); 
		}
		String screenId= screenItemDescriptionList.get(0).getScreenId();
		String designTypeId= screenItemDescriptionList.get(0).getDesignTypeId();
		List<String> UnauthorityItemList = integrityCheckService.getUnauthorityItem(screenId);
		
		if(UnauthorityItemList.isEmpty()) {
			return Collections.emptyList();  
		}
		
		List<ItemControlEntity> erritemControlList = new ArrayList<>();
		for(String section:UnauthorityItemList) {
			List<ItemControlEntity> erritemControl = integrityCheckService.getItemControl(designTypeId,screenId,Integer.valueOf(section));
			if(erritemControl.isEmpty()) {
				continue;
			}
			erritemControlList.addAll(erritemControl);
		}
		
		if(erritemControlList.isEmpty()) {
			return Collections.emptyList();  
		}

		return erritemControlList;
	}
}
